package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.ProductionLine;
import com.sdry.service.llm.ProductionLineService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 生产线controller
 * @Title:ProductionLineController
 * @Package com.sdry.web.controller.llm
 * @author llm
 * @date 2018年12月26日
 * @version 1.0
 */

@Controller
@RequestMapping(value="/lineMana")
public class ProductionLineController {

	@Resource
	private ProductionLineService lineService;
	
	/**
	 * 分页查询生产线
	 * @Title:listPageProductionLine   
	 * @param page
	 * @param productionLine
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listPageProductionLine")
	public void listPageProductionLine(Page page, ProductionLine productionLine, HttpServletResponse response)throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("page", page.getPage());
		map.put("limit", page.getLimit());
		if(null != productionLine.getLineName() && !"".equals(productionLine.getLineName())){
			map.put("lineName", productionLine.getLineName());
		}else{
			map.put("lineName", "");
			productionLine.setLineName("");
		}
		int count = lineService.countProductionLine(productionLine);
		List<ProductionLine> lineList = lineService.listPageProductionLine(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(lineList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存/修改 产线信息
	 * @Title:saveProductionLine   
	 * @param productionLine
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/saveProductionLine")
	public void saveProductionLine(ProductionLine productionLine, HttpServletResponse response)throws Exception{
		int isSuccess = 0;
		if (productionLine.getLineId() == null || "".equals(productionLine.getLineId())) {
			isSuccess = lineService.saveProductionLine(productionLine);
		} else {
			isSuccess = lineService.updateProductionLine(productionLine);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除产线信息
	 * @Title:saveProductionLine   
	 * @param str
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteProductionLine")
	public void deleteProductionLine(String str, HttpServletResponse response)throws Exception{
		int isSuccess = 0;
		isSuccess = lineService.deleteProductionLine(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 根据名称查询产线信息
	 * @Title:saveProductionLine   
	 * @param str
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/getProductionLineByName")
	public void getProductionLineByName(String lineName, HttpServletResponse response)throws Exception{
		List<ProductionLine> lineList = lineService.getProductionLineByName(lineName);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(lineList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 根据编号查询产线信息
	 * @Title:saveProductionLine   
	 * @param str
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/getProductionLineByNumber")
	public void getProductionLineByNumber(String lineNumber, HttpServletResponse response)throws Exception{
		List<ProductionLine> lineList = lineService.getProductionLineByNumber(lineNumber);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(lineList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
}
